﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DatabaseSchemaReader;
using DatabaseSchemaReader.DataSchema;

namespace Happy.Edge.DatabaseSchemaReader.Extentions
{
    public static class DatbaseColumnExtentions
    {
        public static object ToDynamic(this DatabaseColumn column)
        {
            return new
            {
                dataType = column.DataType.ToDynamic(),
                dateTimePrecision = column.DateTimePrecision,
                dbDataType = column.DbDataType,
                defaultValue = column.DefaultValue,
                foreignKeyTableName = column.ForeignKeyTableName,
                identityIncrement = column.IdentityIncrement,
                identitySeed = column.IdentitySeed,
                isForeignKey = column.IsForeignKey,
                isIdentity = column.IsIdentity,
                isIndexed = column.IsIndexed,
                isIsPrimaryKey = column.IsPrimaryKey,
                isUniqueKey = column.IsUniqueKey,
                length = column.Length,
                name = column.Name,
                netName = column.NetName,
                nullable = column.Nullable,
                nullableIndicator = column.GetNullableIndicator(),
                ordinal = column.Ordinal,
                precision = column.Precision,
                scale = column.Scale,
                tableName = column.TableName
            };
        }

        private static string GetNullableIndicator(this DatabaseColumn column)
        {
            if ((column.DataType.IsDateTime
                 |
                 column.DataType.IsFloat
                 |
                 column.DataType.IsInt
                 |
                 column.DataType.IsNumeric
                 |
                 column.DataType.IsInt
                 |
                 column.DataType.NetDataType == typeof(Guid).FullName
                 |
                 column.DataType.NetDataType == typeof(Boolean).FullName   
                 |
                 column.DataType.NetDataType == typeof(TimeSpan).FullName)
                &
                (column.Nullable))
            {
                return "?";
            }

            return string.Empty;
        }
    }
}
